﻿using System;
using System.Web;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class Offices : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("UC1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
                //if (Session["OFFICE_TYPE"].ToString() != "COR" && Application["Debug"].ToString()!="1")
                //{
                //    Response.Redirect("unauthorized.aspx");
                //}
                txtOfficeId.Text = Session["AIRLINE"].ToString().Trim();
                try
                {
                    lstCurrencies.SelectedValue = Session["DEFAULT_CURRENCY"].ToString();
                    lstCurrencies2.SelectedValue = Session["DEFAULT_CURRENCY"].ToString();
                    lstDefCurrency2.SelectedValue = Session["DEFAULT_CURRENCY"].ToString();
                }
                catch (Exception ex)
                {
                    //Does nothing!
                }
            }
            btnSetStatus.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            //btnSetMaxSeats.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnSetValidDate.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnSetRank.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnAdd.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnSetDeposit.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnAddDeposit.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            int ret_code;
            if (Session["ACL"].ToString().IndexOf("UC2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            try
            {
                DateTime.Parse(txtValidDate.Text);
                DateTime.Parse(txtExpiredDate.Text);
                Double.Parse(txtBalance.Text);
            }
            catch
            {
                lblError.Text = "Invalid date/deposit_balance format!";
                return;
            }

            txtOfficeId.Text = txtOfficeId.Text.Trim().ToUpper();
            if (txtOfficeId.Text.IndexOf(Session["AIRLINE"].ToString().Trim()) != 0)
            {
                lblError.Text = "Office ID should be led by Airline ID!";
                txtOfficeId.Text = Session["AIRLINE"].ToString().Trim() + txtOfficeId.Text;
                return;
            }
            if (txtOfficeId.Text.IndexOf(",") >= 0)
            {
                lblError.Text = "Office ID should not include comma( ,)!";
                return;
            }
            try
            {
                SqlCommand cmd = new SqlCommand("bll_office_new");
                SqlParameter para = cmd.Parameters.AddWithValue("@office", txtOfficeId.Text);
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@type", lstOfficeTypes.SelectedValue);
                para = cmd.Parameters.AddWithValue("@name", txtName.Text);
                para = cmd.Parameters.AddWithValue("@rank", txtRank.Text.ToUpper());
                //if(chkGroup.Checked)
                //    para = cmd.Parameters.AddWithValue("@group_book", "Y");
                //else
                //    para = cmd.Parameters.AddWithValue("@group_book", "N");
                para = cmd.Parameters.AddWithValue("@group_book", chkGroup.Checked);
                para = cmd.Parameters.AddWithValue("@default_currency", lstDefCurrency.SelectedValue);
                para = cmd.Parameters.AddWithValue("@valid_date", txtValidDate.Text);
                para = cmd.Parameters.AddWithValue("@expired_date", txtExpiredDate.Text);
                para = cmd.Parameters.AddWithValue("@status", "L");//新建Office为锁定状态！
                //para = cmd.Parameters.AddWithValue("@max_seats", 999999);//txtMaxSeats.Text);
                if (lstOfficeTypes.SelectedValue == "DEP")
                    para = cmd.Parameters.AddWithValue("@dcs_airport", txtDcsAirport.Text.Trim().ToUpper()); //lstDcsAirport.SelectedValue);
                else
                    para = cmd.Parameters.AddWithValue("@dcs_airport", "----");
                para = cmd.Parameters.AddWithValue("@chk_deposit", 0/*chkDeposit.Checked*/);
                para = cmd.Parameters.AddWithValue("@deposit_currency", "USD"/*lstCurrencies.SelectedValue*/);
                para = cmd.Parameters.AddWithValue("@deposit_balance", 0/*txtBalance.Text*/);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Adding failed!";
                else if (ret_code == 2)
                    lblError.Text = "Can not add more office! Offices reach system limit!";
                else
                    lblError.Text = "Unknow Error(" + ret_code.ToString() + ")";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }

        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("UC2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            string key;
            //string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the office.";
                return;
            }
            key = Request.Form["KEY"];
            //strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_office_delete";
                SqlParameter para = cmd.Parameters.AddWithValue("@office", key);
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Users not empty!";
                else if (ret_code == 2)
                    lblError.Text = "Action failed!";
                else
                    lblError.Text = "Unknow Error(" + ret_code.ToString() + ")";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        protected void btnSetStatus_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("UC2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            string key;
            //string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the office.";
                return;
            }
            key = Request.Form["KEY"];
            //strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_office_set_status";
                SqlParameter para = cmd.Parameters.AddWithValue("@office", key);
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@status", lstStatus.SelectedValue);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                para = cmd.Parameters.AddWithValue("@default_currency", lstDefCurrency2.SelectedValue);

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Action failed!";
                else
                    lblError.Text = "Error Code = " + ret_code.ToString() + " !";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        protected void btnSetMaxSeats_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("UC2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            string key;
            //string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the office.";
                return;
            }
            key = Request.Form["KEY"];
            //strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "usp_set_office_max_seats";
                SqlParameter para = cmd.Parameters.AddWithValue("@office", key);
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@max_seats", txtMaxSeats2.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Action failed!";
                else
                    lblError.Text = "Error Code = " + ret_code.ToString() + " !";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }

        protected void btnSetValidDate_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("UC2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            try
            {
                DateTime.Parse(txtValidDate2.Text);
                DateTime.Parse(txtExpiredDate2.Text);
            }
            catch
            {
                lblError.Text = "Invalid date format!";
                return;
            }

            int ret_code;
            string key;
            //string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the office.";
                return;
            }
            key = Request.Form["KEY"];
            //strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_office_set_valid_date";
                SqlParameter para = cmd.Parameters.AddWithValue("@office", key);
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@valid_date", txtValidDate2.Text);
                para = cmd.Parameters.AddWithValue("@expiry_date", txtExpiredDate2.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Action failed!";
                else
                    lblError.Text = "Error Code = " + ret_code.ToString() + " !";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }
        protected void btnSetRank_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("UC2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            string key;
            //string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the office.";
                return;
            }
            key = Request.Form["KEY"];
            //strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_office_set_office_rank";
                SqlParameter para = cmd.Parameters.AddWithValue("@office", key);
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@rank", txtRank2.Text.ToUpper());
                para = cmd.Parameters.AddWithValue("@group_book", chkGroup2.Checked);
                para = cmd.Parameters.AddWithValue("@chk_deposit", 0/*chkDeposit2.Checked*/);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Action failed!";
                else
                    lblError.Text = "Error Code = " + ret_code.ToString() + " !";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }


        }

        protected void btnUsers_Click(object sender, EventArgs e)
        {
            //string domain = TinyDAL.GetRequestDomain(HttpContext.Current.Request.Url.OriginalString);
            //int port;
            //if (HttpContext.Current.Request.Url.Port != null)
            //    port = HttpContext.Current.Request.Url.Port;
            //else
            //    port = -1;
            string key;
            //string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the office.";
                return;
            }
            key = Request.Form["KEY"];
            //导致参数没有传递倒目标页面
            if (Application["DEBUG"].ToString() == "1")
                //Response.Redirect(Application["http"] + "/users.aspx?KEY=" + key);
                Response.Redirect("~/Users.aspx?KEY=" + key);
            else
            {
                //Response.Redirect(Application["https"] + "/users.aspx?KEY=" + key);
                //if (port != -1)
                //{
                //    domain.Replace(":" + port.ToString(), "");
                //}
                Response.Redirect("https://" + HttpContext.Current.Request.Url.Host + ":" + Convert.ToInt32(Application["HTTPS_PORT"].ToString()).ToString() + Application["WEB_PATH"].ToString() + "/Users.aspx?KEY=" + key);
            }
        }
        protected void btnRoles_Click(object sender, EventArgs e)
        {
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the office.";
                return;
            }
            Response.Redirect("Roles.aspx?office=" + Request.Form["KEY"]);
        }
        protected void btnSetDeposit_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("UC2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            try
            {
                Double.Parse(txtBalance2.Text);
            }
            catch
            {
                lblError.Text = "Invalid deposit balance format!";
                return;
            }
            int ret_code;
            string key;
            //string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the office.";
                return;
            }
            key = Request.Form["KEY"];
            //strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_office_set_deposit";
                SqlParameter para = cmd.Parameters.AddWithValue("@office", key);
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                //para = cmd.Parameters.AddWithValue("@chk_deposit", chkDeposit2.Checked);
                para = cmd.Parameters.AddWithValue("@deposit_currency", lstCurrencies2.SelectedValue);
                para = cmd.Parameters.AddWithValue("@deposit_balance", txtBalance2.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Action failed!";
                else
                    lblError.Text = "Error Code = " + ret_code.ToString() + " !";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnAddDeposit_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("UC2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            try
            {
                Double.Parse(txtBalance2.Text);
            }
            catch
            {
                lblError.Text = "Invalid deposit balance format!";
                return;
            }
            int ret_code;
            string key;
            //string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the office.";
                return;
            }
            key = Request.Form["KEY"];
            //strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_office_add_deposit";
                SqlParameter para = cmd.Parameters.AddWithValue("@office", key);
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@deposit_currency", lstCurrencies2.SelectedValue);
                para = cmd.Parameters.AddWithValue("@deposit_balance", txtBalance2.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Action failed! Check quota currency.";
                else
                    lblError.Text = "Error Code = " + ret_code.ToString() + " !";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnLog_Click(object sender, EventArgs e)
        {
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the office.";
                return;
            }
            Response.Redirect("OfficeLog.aspx?office=" + Request.Form["KEY"]);
        }

    }
}